package cn.com.hbhgkj.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.hbhgkj.ApplicationFormType;
import cn.com.hbhgkj.Constants;
import cn.com.hbhgkj.ReviewCode;
import cn.com.hbhgkj.dao.CopyrightAlterationDao;
import cn.com.hbhgkj.dao.CopyrightRegisterationDao;
import cn.com.hbhgkj.model.CopyrightAlteration;
import cn.com.hbhgkj.model.CopyrightRegisteration;
import cn.com.hbhgkj.model.User;
import cn.com.hbhgkj.service.ApplicationSendException;
import cn.com.hbhgkj.service.CopyrightAlterationManager;
import cn.com.hbhgkj.util.CopyrightSender;
import cn.com.hbhgkj.util.NameValuePairsFactory;

@Service("copyrightAlterationManager")
@WebService(serviceName = "CopyrightAlterationService", endpointInterface = "cn.com.hbhgkj.service.CopyrightAlterationManager")
public class CopyrightAlterationManagerImpl extends
		GenericManagerImpl<CopyrightAlteration, Long> implements
		CopyrightAlterationManager {
	CopyrightAlterationDao copyrightAlterationDao;

	public CopyrightAlterationManagerImpl() {

	}

	@Autowired
	public CopyrightAlterationManagerImpl(
			CopyrightAlterationDao copyrightAlterationDao) {
		super(copyrightAlterationDao);
		this.copyrightAlterationDao = copyrightAlterationDao;
	}

	@Override
	public void review(User reviewer, CopyrightAlteration copyrightAlteration,ReviewCode code)
			throws ApplicationSendException, IllegalArgumentException,
			IllegalAccessException {
		// TODO validate reviewer if is valid
		CopyrightAlteration entity = copyrightAlterationDao
				.get(copyrightAlteration.getId());

		// 审核人更改
		String[] ignoreCopy = new String[] { "applicant", "applyTime",
				"loginId" };
		BeanUtils.copyProperties(copyrightAlteration, entity, ignoreCopy);

		entity.setState(code.getValue());
		entity.setReviewer(reviewer);
		entity.setUpdateTime(new Date());

		if (!ReviewCode.isPass(code.getValue()))
			return;

		// send form to copyright center
		CopyrightSender.login(Constants.AGENT_USERNAME,
				Constants.AGENT_PASSWORD);

		NameValuePairsFactory paramsFactory = new NameValuePairsFactory(entity);

		// 发送表单至copyright中心
		// get serial number
		String serialNum = CopyrightSender.send(
				paramsFactory.createNameValuePairs(),
				CopyrightSender.r41PostUrl);
		CopyrightRegisteration cr = buildCopyrightRegisteration(serialNum,
				entity);
		// 保存我的登记
		copyrightRegisterationDao.save(cr);
	}

	private CopyrightRegisteration buildCopyrightRegisteration(
			String serialNum, CopyrightAlteration copyright) {
		CopyrightRegisteration cr = new CopyrightRegisteration();
		cr.setApplyFormId(copyright.getId());
		cr.setApplyFormType(ApplicationFormType.R11.getValue());
		cr.setContact(copyright.getSqrcontactPerson());
		cr.setCopyrighter(copyright.getCopyrighter());
		cr.setEmail(copyright.getSqremail());
		cr.setPhoneNum(copyright.getSqrphone());
		cr.setProductName(copyright.getSoftFullName());
		cr.setMobileNum(copyright.getSqrmobile());
		cr.setSerialNum(serialNum);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		cr.setRegistDate(sdf.format(new Date()));
		return cr;
	}

	@Autowired
	private CopyrightRegisterationDao copyrightRegisterationDao;

	public CopyrightRegisterationDao getCopyrightRegisterationDao() {
		return copyrightRegisterationDao;
	}

	public void setCopyrightRegisterationDao(
			CopyrightRegisterationDao copyrightRegisterationDao) {
		this.copyrightRegisterationDao = copyrightRegisterationDao;
	}

	@Override
	public List<CopyrightAlteration> findByApplicant(User applicant) {
		return this.copyrightAlterationDao.findByApplicant(applicant);
	}

}